package com.zbzbzzz.image.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author ：zbzbzzz
 * @Date ：Created in 10:41 2021/3/4
 * @Description：_____
 * @Modified By：
 * @Version: X.X
 */
@RestController
@CrossOrigin
@RequestMapping("/")
public class IndexController {

    @PostMapping("searchImage")
    public void searchImage(MultipartFile localFile,String remoteFile,HttpServletRequest request)throws IOException {
        if (localFile==null && StrUtil.isBlank(remoteFile)){
            throw new RuntimeException("搜图接口不支持同时拥有两个参数");
        }
        HashMap<String, Object> paramMap = new HashMap<>(1);
        if (localFile!=null){
            String localPath = request.getSession().getServletContext().getRealPath("/"+localFile.getOriginalFilename());
            File file = FileUtil.file(localPath);
            localFile.transferTo(file);

            paramMap.put("fileName",file);
            String result= HttpUtil.post("http://localhost:8082/mockSI", paramMap);

            FileUtil.del(localPath);
        }
        if (StrUtil.isNotBlank(remoteFile)){
            remoteFile = "https://www.baidu.com/img/PCpad_012830ebaa7e4379ce9a9ed1b71f7507.png";
            String fileName = remoteFile.substring(remoteFile.lastIndexOf("/"),remoteFile.length());
            String localPath = request.getSession().getServletContext().getRealPath(fileName);
            HttpUtil.downloadFile(remoteFile, FileUtil.file(localPath));

            paramMap.put("fileName", FileUtil.file(localPath));
            String result= HttpUtil.post("http://localhost:8082/mockSI", paramMap);

            FileUtil.del(localPath);
        }
    }

    @PostMapping("compareImage")
    public void compareImage(MultipartFile localFile,String remoteFile,HttpServletRequest request)throws IOException{

        String fileName = remoteFile.substring(remoteFile.lastIndexOf("/"),remoteFile.length());
        String remotePath = request.getSession().getServletContext().getRealPath(fileName);
        HttpUtil.downloadFile(remoteFile, FileUtil.file(remotePath));

        String localPath = request.getSession().getServletContext().getRealPath("/"+localFile.getOriginalFilename());
        File file = FileUtil.file(localPath);
        localFile.transferTo(file);

        FileUtil.del(localPath);
        FileUtil.del(remotePath);
    }

    @GetMapping("getImages")
    public List<String> getImages(HttpServletRequest request){
        HashMap<String,Object> param = new HashMap<>(4);
        param.put("depth","4");
        param.put("entityId","z");
        param.put("userId","z");
        param.put("xtbs","zjspxt");

        String url = "https://mockapi.eolinker.com/dZYeRfy78e922c923ac2c3d7e8f800c74a8e17b54cc2351/jz/getJzWsml";
        String result = HttpUtil.get(url,param);

        JSONObject jsonObject = JSONUtil.parseObj(result);
        ArrayList<String> list = new ArrayList<>();
        parseSrc(jsonObject,list);
        return list;
    }
    public void parseSrc(JSONObject jsonObject,ArrayList<String> list){
        JSONObject details=jsonObject.getJSONObject("details");
        if(details!=null){
            String src = details.getStr("origin");
            if (StrUtil.isNotBlank(src)){
                list.add(src);
            }
        }
        JSONArray children = jsonObject.getJSONArray("children");
        if(children==null||children.isEmpty()){
            return;
        }
        else{
            for (Object item:children) {
                parseSrc((JSONObject) item,list);
            }
        }
    }

    @GetMapping("test")
    public void test(HttpServletRequest request){
        String fileUrl = "https://www.baidu.com/img/PCpad_012830ebaa7e4379ce9a9ed1b71f7507.png";
        String localPath = request.getSession().getServletContext().getRealPath("/tmp.png");
        System.out.println(localPath);
        HttpUtil.downloadFile(fileUrl, FileUtil.file(localPath));
    }

    @PostMapping("/mockSI")
    public String mockSearchImage(MultipartFile fileName){
        String result = " {\n" +
                "    \"success\": true,\n" +
                "    \"code\": 200,\n" +
                "    \"message\": \"庭中证据材料比对成功\",\n" +
                "    \"data\": [{\n" +
                "      \"name\": \"pic1.jpg\",\n" +
                "      \"score\": \"9\",\n" +
                "      \"url\": \"http://xx.xx.xxx\",\n" +
                "      \"url_thumb\": \"http://xx.xxx.xxx\" ,\n" +
                "    },\n" +
                "\t{\n" +
                "      \"name\": \"pic1.jpg\",\n" +
                "      \"score\": \"9\",\n" +
                "      \"url\": \"http://xx.xx.xxx\",\n" +
                "      \"url_thumb\": \"http://xx.xxx.xxx\" ,\n" +
                "    },\n" +
                "\t{\n" +
                "      \"name\": \"pic1.jpg\",\n" +
                "      \"score\": \"9\",\n" +
                "      \"url\": \"http://xx.xx.xxx\",\n" +
                "      \"url_thumb\": \"http://xx.xxx.xxx\" ,\n" +
                "    },\n" +
                "\t{\n" +
                "      \"name\": \"pic1.jpg\",\n" +
                "      \"score\": \"9\",\n" +
                "      \"url\": \"http://xx.xx.xxx\",\n" +
                "      \"url_thumb\": \"http://xx.xxx.xxx\" ,\n" +
                "    },\n" +
                "\t{\n" +
                "      \"name\": \"pic1.jpg\",\n" +
                "      \"score\": \"9\",\n" +
                "      \"url\": \"http://xx.xx.xxx\",\n" +
                "      \"url_thumb\": \"http://xx.xxx.xxx\" ,\n" +
                "    }\n" +
                "    ]\n" +
                "  }";
        return result;
    }

    @GetMapping("/mockGF")
    public void mockGetFiles(){}

    @PostMapping("/mockCI")
    public void mockCompareImage(){}
}
